// pages/msg/msg.js
import config from "../../../config.js";
const base = config;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    isEdit: true,
    user: '',
    sex: ["女", "男"],
    userHeader: '',
    userName: '',
    userICNO: '',
    userSex: '',
    userBir: "",
    sexIndex: 0,
    //
    idenCardA: "https://app.longchg.com/gudongcar/images/idenCardA.png",
    idenCardB: "https://app.longchg.com/gudongcar/images/idenCardB.png",

    //行驶证
    licenseFr: "https://app.longchg.com/gudongcar/images/licenseA.png",
    licenseBa: "https://app.longchg.com/gudongcar/images/licenseB.png",

    isUploadFr: false,
    isUploadBa: false,
    isUploadIDFr: false,
    isUploadIDBa: false,
    isUploadFirst: true,
    isUploadBaFirst: true,
    isUploadIDFirst: true,
    isUploadBaIDFirst: true,


  },

  //姓名
  nameChange: function(e) {
    this.setData({
      userName: e.detail.value
    })
  },


  //身份证
  ICNOChange: function(e) {
    var item = e.detail.value;
    this.setData({
      userICNO: e.detail.value
    })

    if (item.length > 14) {
      var birth = item.substring(6, 14);
      var year = birth.substring(0, 4);
      var month = birth.substring(4, 6);
      var day = birth.substring(6, 8);
      birth = year + "-" + month + "-" + day;
      this.setData({
        userBir: birth
      })
    }



  },

  //性别
  sexChange: function(e) {
    console.log(e);
    this.setData({
      userSex: ["女", "男"][e.detail.value],
      sexIndex: e.detail.value
    })
  },


  //生日
  birthChange: function(e) {
    this.setData({
      userBir: e.detail.value
    })
  },

  //头像
  headerChange: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        that.setData({
          userHeader: res.tempFilePaths[0],
        })

        const tempFilePaths = res.tempFilePaths;
        wx.navigateTo({
          url: '../imageUtil/imageUtil?src=' + tempFilePaths[0] + "&key=userHeader",
        })

        //上传正面
        /** wx.showLoading({
           title: '正在上传',
         })
         wx.uploadFile({
           url: base.uploadPic,
           filePath: tempFilePaths[0],
           name: 'picA',
           header: {
             "Cookie": wx.getStorageSync("session"),
             "content-type": "multipart/form-data"
           },
           success: function(res) {
             wx.hideLoading();
             res = JSON.parse(res.data);
             console.log(res);
             if (res.status == 200) {
               that.setData({
                 userHeader: res.fileName
               })

             } else {
             }
           }
         })**/
      },
    })
  },


  edit: function(result) {
    var flag = this.data.isEdit,
      session = wx.getStorageSync("session"),
      that = this;

    var userHeader = this.data.userHeader,
      userTel = this.data.userTel,
      userICNO = this.data.userICNO,
      userBir = this.data.userBir,
      userName = this.data.userName,
      sexIndex = this.data.sexIndex;



    if (!flag) {
      this.setData({
        isEdit: true
      })
    } else {
      //提交
      //身份证号
      var reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/;
      if (userICNO) {
        if (!reg.test(userICNO)) {
          wx.showToast({
            title: '身份证号码格式不正确',
            icon: 'none'
          })
          return false;
        }
      }
      var idCardA = this.data.idenCardA == "https://app.longchg.com/gudongcar/images/idenCardA.png" ? "" : this.data.idenCardA;
      var idCardB = this.data.idenCardB == "https://app.longchg.com/gudongcar/images/idenCardB.png" ? "" : this.data.idenCardB;
      var licenseFr = this.data.licenseFr == "https://app.longchg.com/gudongcar/images/licenseA.png" ? "" : this.data.licenseFr;
      var licenseBa = this.data.licenseBa == "https://app.longchg.com/gudongcar/images/licenseB.png" ? "" : this.data.licenseBa;
      wx.request({
        url: base.changeUserInfo,
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': session
        },
        data: {
          image: userHeader,
          name: userName,
          gender: sexIndex,
          birth: userBir,
          idenCardNo: userICNO,
          idenCardA: idCardA,
          idenCardB: idCardB,
          licenseFr: licenseFr,
          licenseBa: licenseBa,


        },
        success: function(res) {
          if (res.data.status == 200) {
            var user = res.data.data;
            wx.setStorageSync("user", user);
            if (result != false) {
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000);
            }

          } else {
            wx.showToast({
              title: '信息更新失败',
              icon: 'none'
            })
          }
        }
      })



      this.setData({
        isEdit: true
      })
    }
  },

  //行驶证
  uploadFr: function() {
    let that = this;
    if (!that.data.isEdit) {
      return;
    }
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseFr: tempFilePaths,
          isUploadFr: true,
          isUploadFirst: false
        })

        console.log(that.data.licenseFr);


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.licenseFr[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadFr: true,
              })
              that.data.licenseFr = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBa: function() {
    let that = this;
    if (!that.data.isEdit) {
      return;
    }
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          licenseBa: tempFilePaths,
          isUploadBa: true,
          isUploadBaFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.licenseBa[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadBa: true,
              })
              that.data.licenseBa = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },
  //身份证正面
  uploadFrID: function() {
    let that = this;
    if (!that.data.isEdit) {
      return;
    }
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardA: tempFilePaths,
          isUploadIDFr: true,
          isUploadIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.idenCardA[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            res = JSON.parse(res.data);
            console.log(res);
            if (res.status == 200) {
              that.setData({
                isUploadIDFr: true,
              })
              that.data.idenCardA = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },

  uploadBaID: function() {
    let that = this;
    if (!that.data.isEdit) {
      return;
    }
    wx.chooseImage({
      count: 1,
      success: function(res) {
        const tempFilePaths = res.tempFilePaths;
        that.setData({
          idenCardB: tempFilePaths,
          isUploadIDBa: true,
          isUploadBaIDFirst: false
        })


        //上传正面
        wx.showLoading({
          title: '正在上传',
        })
        wx.uploadFile({
          url: base.uploadPic,
          filePath: that.data.idenCardB[0],
          name: 'picA',
          header: {
            "Cookie": wx.getStorageSync("session"),
            "content-type": "multipart/form-data"
          },
          success: function(res) {
            wx.hideLoading();
            console.log(res);
            res = JSON.parse(res.data);
            if (res.status == 200) {
              that.setData({
                isUploadIDBa: true,
              })
              that.data.idenCardB = res.fileName;
            } else {
              wx.showToast({
                title: res.message + '',
                icon: 'none',
                mask: true,
                duration: 2000
              })
            }
          }
        })
      },
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var user = wx.getStorageSync("user");
    //处理手机
    var tel = user.tel;
    tel = tel.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
    this.setData({
      secTel:tel,
      user: user,
      userBir: wx.getStorageSync("user").birth,
    })
    if (user.identityCardA != null && user.identityCardA != "") {
      this.setData({
        idenCardA: user.identityCardA,
        isUploadIDFirst: false,

      })
    }
    if (user.identityCardB != null && user.identityCardB != "") {
      this.setData({
        idenCardB: user.identityCardB,
        isUploadBaIDFirst: false,

      })
    }
    if (user.licenseA != null && user.licenseA != "") {
      this.setData({
        licenseFr: user.licenseA,
        isUploadFirst: false,

      })
    }
    if (user.licenseB != null && user.licenseB != "") {
      this.setData({
        licenseBa: user.licenseB,
        isUploadBaFirst: false,
      })
    }

    if (user.birth != null && user.birth != "") {
      this.setData({
        userBir: user.birth
      })
    }

    if (this.data.userBir == null || this.data.userBir == "") {
      this.setData({
        userBir: '1990-01-01'
      })
    }

    if (user.identityCard != null && user.identityCard != "") {
      var item = user.identityCard;
      if (item.length > 14) {
        var birth = item.substring(6, 14);
        var year = birth.substring(0, 4);
        var month = birth.substring(4, 6);
        var day = birth.substring(6, 8);
        birth = year + "-" + month + "-" + day;
        console.log(birth);
        this.setData({
          userBir: birth
        })
      }
    }

    if (user.sex != null && user.sex != "") {
      this.setData({
        sexIndex: user.sex
      })
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var self = this;

    var key = wx.getStorageSync("userHeader");
    if (key != null && key != "") {
      console.log(key);
      self.uploadHeaderImage(key);
    }
  },
  uploadHeaderImage: function(src) {
    var that = this;
    wx.uploadFile({
      url: base.uploadPic,
      filePath: src,
      name: 'picA',
      header: {
        "Cookie": wx.getStorageSync("session"),
        "content-type": "multipart/form-data"
      },
      success: function(res) {
        wx.hideLoading();
        res = JSON.parse(res.data);
        console.log(res);
        if (res.status == 200) {
          /**that.setData({
            userHeader: res.fileName
          })**/

          wx.setStorageSync("userHeader", null);

          that.edit(false);


        } else {}
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})